package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.service.PaymentFeignService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "paymentGlobalFallback")
public class OrderController {

    @Resource
    private PaymentFeignService paymentService;

    @GetMapping("/")
    public String index() {
        return "consumer -> eureka + ribbon + feign";
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult get(@PathVariable("id") Long id) {
        return paymentService.getPaymentById(id);
    }



    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        return paymentService.paymentInfo_OK(id);
    }

    /*@HystrixCommand(fallbackMethod = "paymentInfoHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "300")
    })*/
    @HystrixCommand
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    public String paymentInfo_Timeout(@PathVariable("id") Integer id) {
        return paymentService.paymentInfo_Timeout(id);
    }

    // callback paymentInfo_Timeout
    public String paymentInfoHandler(Integer id) {
        return "服务端繁忙, 超时时间 1.5秒";
    }

    // global callback
    public String paymentGlobalFallback() {
        return "服务器繁忙, 请稍后再试!";
    }


    // 该方法的服务降级由feign.hystrix实现
    @GetMapping("/consumer/payment/feign/hystrix/timeout/{id}")
    public String paymentInfo_Timeout02(@PathVariable("id") Integer id) {
        return paymentService.paymentInfo_Timeout(id);
    }
}
